package com.example.baseweb.controller;

import com.example.baseweb.common.ApiResponse;
import com.example.baseweb.common.PageResponse;
import com.example.baseweb.entity.User;
import com.example.baseweb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    // 获取所有用户（分页）
    @PostMapping("/page")
    public ApiResponse<PageResponse<User>> getUsersPage(@RequestBody Map<String, Object> params) {
        int page = params.get("page") != null ? Integer.valueOf(params.get("page").toString()) : 1;
        int size = params.get("size") != null ? Integer.valueOf(params.get("size").toString()) : 10;
        String nickname = params.get("nickname") != null ? params.get("nickname").toString() : null;
        String phone = params.get("phone") != null ? params.get("phone").toString() : null;
        return ApiResponse.success(userService.getUserPage(page, size, nickname, phone));
    }
    
    // 根据ID获取用户
    @PostMapping("/detail")
    public ApiResponse<User> getUserById(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        return ApiResponse.success(userService.getUserById(id));
    }
    
    // 根据openid获取用户
    @PostMapping("/getByOpenid")
    public ApiResponse<User> getUserByOpenid(@RequestBody Map<String, Object> params) {
        String openid = params.get("openid").toString();
        return ApiResponse.success(userService.getUserByOpenid(openid));
    }
    
    // 更新用户状态
    @PostMapping("/updateStatus")
    public ApiResponse<User> updateUserStatus(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        Integer status = Integer.valueOf(params.get("status").toString());
        return ApiResponse.success(userService.updateUserStatus(id, status));
    }
    
    // 根据状态查询用户
    @PostMapping("/listByStatus")
    public ApiResponse<List<User>> getUsersByStatus(@RequestBody Map<String, Object> params) {
        Integer status = Integer.valueOf(params.get("status").toString());
        return ApiResponse.success(userService.getUsersByStatus(status));
    }
} 